//
//  RootViewController.m
//  JiaYiPu
//
//  Created by 葛承铭 on 2019/5/6.
//  Copyright © 2019 JiaYiPu. All rights reserved.
//

#import "RootViewController.h"
#import <Masonry.h>
#import <WebKit/WebKit.h>

@interface RootViewController ()<WKUIDelegate,WKNavigationDelegate>

@property (nonatomic, strong) UIButton *refreshBtn;

@property (nonatomic, weak) UILabel *noNetLab;

@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic, strong) NSURLRequest *request;


@end

@implementation RootViewController


- (NSURLRequest *)request
{
    if (!_request) {
        _request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://101.132.25.15:8001/DeLongJiGuangApp/View/login.html"]];
    }
    return _request;
}


- (UIButton *)refreshBtn
{
    if (!_refreshBtn) {
        _refreshBtn = [[UIButton alloc] init];
        [_refreshBtn setImage:[UIImage imageNamed:@"icon_refresh_circle"] forState:UIControlStateNormal];
        [_refreshBtn addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
    }
    return _refreshBtn;
}

- (WKWebView *)webView
{
    if (!_webView) {
        _webView = [[WKWebView alloc] init];
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.scrollView.showsVerticalScrollIndicator = NO;
        _webView.scrollView.showsHorizontalScrollIndicator = NO;
        _webView.navigationDelegate = self;
    }
    return _webView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.refreshBtn.hidden = YES;
    


    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.view).offset(20);
    }];
    
    [self.view addSubview:self.refreshBtn];
    [self.refreshBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.centerY.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(38, 38));
    }];
    
    
    UILabel *noNetLab = [[UILabel alloc] init];
    noNetLab.text = @"请检查网络后重试！";
    noNetLab.hidden = YES;
    noNetLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noNetLab];
    self.noNetLab = noNetLab;
    [noNetLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.refreshBtn.mas_bottom).offset(20);
    }];
    
    [self.webView loadRequest:self.request];
    
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    self.refreshBtn.hidden = NO;
    self.noNetLab.hidden = NO;
}

- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    self.noNetLab.hidden = NO;
    self.refreshBtn.hidden = NO;
}


- (void)refresh
{
    self.refreshBtn.hidden = YES;
    self.noNetLab.hidden = YES;
    [self.webView loadRequest:self.request];

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
